package com.mediacloud.app.newsmodule.fragment.baoliao.holder;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.model.news.kt.TypeX;
import com.mediacloud.app.newsmodule.fragment.activity.VideoPreActivity;
import com.mediacloud.app.newsmodule.model.baoliao.list.list.BaoNiaoListItem;
import com.mediacloud.app.newsmodule.model.baoliao.list.list.BaoNiaoMedia;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.utils.ViewUtils;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapter;
import com.mediacloud.app.newsmodule.activity.microlive.XdNineGridDivider;
import com.mediacloud.app.newsmodule.fragment.baoliao.activity.BaoLiaoDetailActivity;
import com.mediacloud.app.newsmodule.fragment.baoliao.adapter.BaoLiaoListGridAdapter;
import com.mediacloud.app.newsmodule.fragment.baoliao.adapter.BaoLiaoRankListAdapter;
import com.mediacloud.app.model.news.BaoLiaoMeta;
import com.mediacloud.app.newsmodule.utils.GridLayoutSpanCountUtils;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;

import org.json.JSONArray;
import org.json.JSONObject;

public class BaoLiaoRankListImgVideoHolder extends BaoLiaoRankListPureTxtHolder
        implements BaoLiaoRankListAdapter.BaoliaoInterface
        , BaseRecyclerAdapter.ItemClickListener {
    RecyclerView baoniaogrid;
    BaoLiaoListGridAdapter baoliaogridAdapter;
    GridLayoutManager gridLayoutManager;
    Context context;

    private int lastHeight = -1;

    private boolean hasSetSpanCount = false;

    private String TAG = BaoLiaoRankListImgVideoHolder.class.getSimpleName();

    public BaoLiaoRankListImgVideoHolder(View view, int viewType) {
        this(view);
        hasSetSpanCount = true;
        int model = GridLayoutSpanCountUtils.ItemTypeSpec.getMode(viewType);
        int spanCount = GridLayoutSpanCountUtils.calculateSpanCount(model >> GridLayoutSpanCountUtils.ItemTypeSpec.MODE_SHIFT);
        gridLayoutManager.setSpanCount(spanCount);
        Log.w(TAG, "BaoLiaoNavListImgVideoHolder spanCount " + spanCount + " model " + (model >> GridLayoutSpanCountUtils.ItemTypeSpec.MODE_SHIFT));
        baoliaogridAdapter = new BaoLiaoListGridAdapter(itemView.getContext(), spanCount);
        baoniaogrid.setAdapter(baoliaogridAdapter);

        baoliaogridAdapter.getData().clear();
        for (int i = 0; i < model >> GridLayoutSpanCountUtils.ItemTypeSpec.MODE_SHIFT; i++) {
            baoliaogridAdapter.getData().add(new BaoNiaoMedia());
        }
        baoliaogridAdapter.notifyDataSetChanged();
    }

    public BaoLiaoRankListImgVideoHolder(View view) {
        super(view);
        baoniaogrid = view.findViewById(R.id.baoniaogrid);
        gridLayoutManager = ViewUtils.generateRecyclerGridLayoutManager(view.getContext(), true, 3);
        baoniaogrid.setLayoutManager(gridLayoutManager);
       /* baoniaogrid.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (itemView != null && itemView.getParent() != null && itemView.getParent() instanceof RecyclerView && event.getAction() == MotionEvent.ACTION_UP)
                    itemView.performClick();
                return false;
            }
        });*/

        baoniaogrid.addItemDecoration(new XdNineGridDivider(itemView.getContext().getResources().getDimensionPixelSize(R.dimen.dimen5)));

    }

    private boolean isRegular = false;

    @Override
    public void updateData(BaoNiaoListItem item, int position, Context context, boolean isRegular) {
        super.updateData(item, position, context, isRegular);
        this.isRegular = isRegular;
        this.context = context;
        itemView.setTag(R.id.tagSaveId, item);
        baoliaogridAdapter.itemClickListener = this;
        if (item.baoNiaoMediaList == null) {
            item.baoNiaoMediaList = BaoNiaoMedia.parseBaoLiaoMediaList(item);
        }
        if (baoliaogridAdapter == null) {
            baoliaogridAdapter = new BaoLiaoListGridAdapter(itemView.getContext());
            baoniaogrid.setAdapter(baoliaogridAdapter);
            baoliaogridAdapter.itemClickListener = this;
        }
        if (!hasSetSpanCount) {
            gridLayoutManager.setSpanCount(GridLayoutSpanCountUtils.calculateSpanCount(item.baoNiaoMediaList.size()));
            baoliaogridAdapter.setSpancount(gridLayoutManager.getSpanCount());
        }
        baoliaogridAdapter.getData().clear();
        baoliaogridAdapter.getData().addAll(item.baoNiaoMediaList);
        baoliaogridAdapter.notifyDataSetChanged();
    }


    @Override
    public void onItemClick(int index, boolean isFromAdaptor, BaseRecyclerAdapter adapter) {
        BaoNiaoListItem baoNiaoListItem = itemView.getTag(R.id.tagSaveId) != null ? (BaoNiaoListItem) itemView.getTag(R.id.tagSaveId) : null;
        if (baoNiaoListItem == null) {
            return;
        }
        if (isRegular) {
            if (baoNiaoListItem.getIs_display() == 0) {
                Log.w("APPTAG", "审核没过的  不能点");
                return;
            }
            Intent intent = new Intent(context, BaoLiaoDetailActivity.class);
            intent.putExtra("id", baoNiaoListItem.getId());
            intent.putExtra("status", baoNiaoListItem.getStatus());
            intent.putExtra("url", baoNiaoListItem.getShareHtml());
            NewsItemClickUtils.startActivity(context, intent);
            return;
        }

        BaoNiaoMedia baoNiaoMedia = baoliaogridAdapter.getItem(index);
        if (baoNiaoMedia.isVideo) {
            BaoLiaoMeta meta = new BaoLiaoMeta();
            meta.setId(baoNiaoListItem.getId());
            meta.setAvatar(baoNiaoListItem.getAvatar());
            meta.setNickName(baoNiaoListItem.getNickName());
            meta.setFavorCount((int) baoNiaoListItem.getFavorCount());
            meta.setVideo(baoNiaoListItem.getVideo());
            meta.setCatalogName(baoNiaoListItem.getCatalogName());
            meta.setTitle(baoNiaoListItem.getTitle());
            meta.setContent(baoNiaoListItem.getContent());
            meta.setCommentCount((int) baoNiaoListItem.getCommentCount());

            Intent intent = new Intent(itemView.getContext(), VideoPreActivity.class);
            if (baoNiaoListItem.getVideo().size() > 0) {
                intent.putExtra("URL", baoNiaoListItem.getVideo().get(0));
                itemView.getContext().startActivity(intent);
            }
        } else {
            try {
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; baoNiaoListItem.getImage() != null && i < baoNiaoListItem.getImage().size(); i++) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("info", baoNiaoListItem.getContent());
                    jsonObject.put("src", baoNiaoListItem.getImage().get(i));
                    jsonArray.put(jsonObject);
                }
                final ArticleItem imgArticle = new ArticleItem();
                imgArticle.setTitle(baoNiaoListItem.getCatalogName());
                imgArticle.setImage(jsonArray.toString());
                imgArticle.setType(TypeX.NEWS.PHOTOS);
                int imgIndex = index;
                if (baoNiaoListItem.getVideo() != null) {
                    imgIndex -= baoNiaoListItem.getVideo().size();
                }
                NewsItemClickUtils.OpenItemNewsHandle(itemView.getContext(), TypeX.NEWS.PHOTOS, imgArticle, new CatalogItem(), imgIndex, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void setLastHeight(int lastHeight) {
        this.lastHeight = lastHeight;
    }
}